﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Watch_It
{
    class Personnage : Element
    {
        //Constante de vie Maximum
        private int _vieMax;

        //Constante de vitesse
        private float _composanteVitesse;

        //Distance pour la translation d'évitement
        private float _distanceTranslation;

        //Etat course
        private float _etatCourse;

        
        //Parametre pour la direction
        private Vector3 N = new Vector3(0f,0f,1f);
        private Vector3 NE = new Vector3(-1f, 0f, 1f);
        private Vector3 E = new Vector3(-1f, 0f, 0f);
        private Vector3 SE = new Vector3(-1f, 0f, -1f);
        private Vector3 S = new Vector3(0f, 0f, -1f);
        private Vector3 SO = new Vector3(1f, 0f, -1f);
        private Vector3 O = new Vector3(1f, 0f, 0f);
        private Vector3 NO = new Vector3(1f, 0f, 1f);

        public Vector3 Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }
        private Vector3 _direction;
        
        public void init()
        {
            _composanteVitesse = 0.2f;
            _vieMax = 10;
            _distanceTranslation = 10f;            
            Vitesse = new Vector3(_composanteVitesse, _composanteVitesse, _composanteVitesse);
            Direction = N;
        }
   
        public int Vie
        {
            get { return _vie; }
            set { _vie = value; }
        }
        private int _vie;

        public void DiminuerVie(int degat)
        {
            _vie -= degat;
            if(_vie < 0)
            {
                _vie=0;
            }
        }

        public void AugmenterVie(int sante)
        {
            _vie += sante;
            if (_vie > _vieMax)
            {
                _vie = _vieMax;
            }
        }

        public Vector3 Vitesse
        {
            get { return _vitesse; }
            set { _vitesse = value; }
        }

        public void VitesseParDefaut()
        {
            Vitesse=new Vector3(_composanteVitesse,_composanteVitesse,_composanteVitesse); 
        }

        private Vector3 _vitesse;

        public void Deplacer(GameTime time)
        {
            if (Position.Y <= 0)
            {
                _etatCourse = 0.1f;
            }
            if (Position.Y >= 1)
            {
                _etatCourse = -0.1f;
            }
            Position = new Vector3(Position.X, Position.Y + _etatCourse, Position.Z);
            Position += Vitesse * Direction * (float)time.ElapsedGameTime.TotalMilliseconds;
        }

        //Sens : 1=droite, -1=gauche
        //Angle: 0 = faible (45degres), 1 = Fort(90degres)
        public void virage(int sens, int angle)
        {
            if ((Direction == NE && sens == -1 && angle == 0) || (Direction == NO && sens == 1 && angle == 0) || (Direction == E && sens == -1 && angle == 1) || (Direction == O && sens == 1 && angle == 1))
            {
                Direction = N;
            }
            else
            {
                if ((Direction == SO && sens == -1 && angle == 0) || (Direction == SE && sens == 1 && angle == 0) || (Direction == O && sens == -1 && angle == 1) || (Direction == E && sens == 1 && angle == 1))
                {
                    Direction = S;
                }
                else
                {
                    if ((Direction == SE && sens == -1 && angle == 0) || (Direction == NE && sens == 1 && angle == 0) || (Direction == S && sens == -1 && angle == 1) || (Direction == N && sens == 1 && angle == 1))
                    {
                        Direction = E;
                    }
                    else
                    {
                        if ((Direction == NO && sens == -1 && angle == 0) || (Direction == SO && sens == 1 && angle == 0) || (Direction == N && sens == -1 && angle == 1) || (Direction == S && sens == 1 && angle == 1))
                        {
                            Direction = O;
                        }
                        else
                        {
                            if ((Direction == O && sens == -1 && angle == 0) || (Direction == S && sens == 1 && angle == 0) || (Direction == NO && sens == -1 && angle == 1) || (Direction == SE && sens == 1 && angle == 1))
                            {
                                Direction = SO;
                            }
                            else
                            {
                                if ((Direction == S && sens == -1 && angle == 0) || (Direction == E && sens == 1 && angle == 0) || (Direction == SO && sens == -1 && angle == 1) || (Direction == NE && sens == 1 && angle == 1))
                                {
                                    Direction = SE;
                                }
                                else
                                {
                                    if ((Direction == E && sens == -1 && angle == 0) || (Direction == N && sens == 1 && angle == 0) || (Direction == SE && sens == -1 && angle == 1) || (Direction == NO && sens == 1 && angle == 1))
                                    {
                                        Direction = NE;
                                    }
                                    else
                                    {
                                        if ((Direction == N && sens == -1 && angle == 0) || (Direction == O && sens == 1 && angle == 0) || (Direction == NE && sens == -1 && angle == 1) || (Direction == SO && sens == 1 && angle == 1))
                                        {
                                            Direction = NO;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
           
            if(sens == 1 && angle == 0)
            {
                Rotation -= 45f;
            }
            if (sens == 1 && angle == 1)
            {
                Rotation -= 90f;
            }
            if (sens == -1 && angle == 0)
            {
                Rotation += 45f;
            }
            if (sens == -1 && angle == 1)
            {
                Rotation += 90f;
            }
        }

        public void TranslationGauche()
        {

            Position += new Vector3(_distanceTranslation, 0, 0);
        }

        public void TranslationDroite()
        {
            Position += new Vector3(-_distanceTranslation, 0, 0);
        }
         
        public void gestionEvenement(KeyboardState clavier, KeyboardState oldClavier)
        {
            if (clavier.IsKeyDown(Keys.Left) && oldClavier.IsKeyUp(Keys.Left))
            {
                virage(-1, 0);
            }
            else
            {
                if (clavier.IsKeyDown(Keys.Right) && oldClavier.IsKeyUp(Keys.Right))
                {
                    virage(1, 0);
                }
                else
                {
                    if (clavier.IsKeyDown(Keys.Up) && oldClavier.IsKeyUp(Keys.Up))
                    {
                        //virage(1, 1);
                        TranslationGauche();
                    }
                    else
                    {
                        if (clavier.IsKeyDown(Keys.Down) && oldClavier.IsKeyUp(Keys.Down))
                        {
                            //virage(-1, 1);
                            TranslationDroite();
                        }
                    }
                }
            }
        }
    }
}
